package com.emagic.manage.oss.glide;


import android.net.Uri;

import com.emagic.manage.data.BuildConfig;
import com.emagic.manage.data.oss.OSSFileHelper;

/**
 * 图片访问规则后缀 <br/>
 * URL规则： http://bucket.<endpoint>/object?x-oss-process=image/action,parame_value
 * bucket：用户的 IMG 频道
 * endpoint：用户的 bucket 所在数据中心的访问域名
 * object：用户上传在 OSS上的图片文件
 * action：用户对图片做的操作
 * parame：用户对图片做的操作所对应的参数
 *
 * @author baoy
 * @version 1.0
 *          Create by 2017/6/26 下午3:23
 */
public class OSSFile {

    private static final Format DEFAULT_FORMAT = Format.jpg;
    private static final String DEFAULT_QUALITY = "70";

    private String name;    //图片名称
    private String type;    //处理类型
    private String quality; //图片质量
    private String format;  //图片格式
    private String orient;  //适应方向
    private String rotate;  //旋转角度

    /**
     * 强制高宽
     *
     * @param name
     * @param quality
     * @param format
     * @return
     */
    public static OSSFile buildFixed(String name, int quality, Format format) {
        OSSFile ossFile = new OSSFile(name);
        ossFile.setType("image/resize,w_%s,h_%s,m_fixed");
        ossFile.setQuality(String.format("/quality,q_%s", quality));
        ossFile.setFormat(String.format("/format,%s", format.toString()));
        ossFile.setOrient("/auto-orient,1"); //自适应
        return ossFile;
    }

    /**
     * 强制高宽
     *
     * @param name
     * @return
     */
    public static OSSFile buildFixed(String name) {
        OSSFile ossFile = new OSSFile(name);
        ossFile.setType("image/resize,w_%s,h_%s,m_fixed");
        ossFile.setQuality(String.format("/quality,q_%s", DEFAULT_QUALITY));
        ossFile.setFormat(String.format("/format,%s", DEFAULT_FORMAT));
        ossFile.setOrient("/auto-orient,1"); //自适应
        return ossFile;
    }

    /**
     * 自动裁剪
     *
     * @param name
     * @param quality
     * @param format
     * @return
     */
    public static OSSFile buildFill(String name, int quality, Format format) {
        OSSFile ossFile = new OSSFile(name);
        ossFile.setType("image/resize,w_%s,h_%s,m_fill");
        ossFile.setQuality(String.format("/quality,q_%s", quality));
        ossFile.setFormat(String.format("/format,%s", format.toString()));
        ossFile.setOrient("/auto-orient,1"); //自适应
        return ossFile;
    }

    public static OSSFile buildFill(String name) {
        OSSFile ossFile = new OSSFile(name);
        ossFile.setType("image/resize,w_%s,h_%s,m_fill");
        ossFile.setQuality(String.format("/quality,q_%s", DEFAULT_QUALITY));
        ossFile.setFormat(String.format("/format,%s", DEFAULT_FORMAT));
        ossFile.setOrient("/auto-orient,1"); //自适应
        return ossFile;
    }

    String buildUrl(int width, int height) {
        return OSSFileHelper.getFileRemoteUrl(name)
                .concat(buildStyle(width, height));
    }

    String buildStyle(int width, int height) {
        StringBuilder sb = new StringBuilder("?x-oss-process=");
        sb.append(String.format(type, width, height))
                .append(orient)
                .append(quality)
                .append(format);
        return sb.toString();
    }


    public OSSFile(String name) {
        this.name = name;
        setType("image/resize,w_%s,h_%s,m_fill");
        setQuality(String.format("/quality,q_%s", DEFAULT_QUALITY));
        setFormat(String.format("/format,%s", DEFAULT_FORMAT));
        setOrient("/auto-orient,1"); //自适应
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }


    public String getQuality() {
        return quality;
    }

    public void setQuality(String quality) {
        this.quality = quality;
    }

    public String getFormat() {
        return format;
    }

    public void setFormat(String format) {
        this.format = format;
    }

    public String getOrient() {
        return orient;
    }

    public void setOrient(String orient) {
        this.orient = orient;
    }

    public String getRotate() {
        return rotate;
    }

    public void setRotate(String rotate) {
        this.rotate = rotate;
    }

    public enum Format {
        jpg, webp, png, bmp
    }

    public static String getRemoteUrl(String filename) {
        if (filename.startsWith("/")) {
            return BuildConfig.CDN_BASE_URL.concat(filename);
        } else {
            return BuildConfig.CDN_BASE_URL.concat("/").concat(filename);
        }
    }

    public static Uri getRemoteUri(String path) {
        if (path.startsWith("/")) {
            return Uri.parse(BuildConfig.CDN_BASE_URL.concat(path));
        } else {
            return Uri.parse(BuildConfig.CDN_BASE_URL.concat("/").concat(path));
        }
    }

}
